<?php
class C_MedicalMaterial extends Controller {
    var $template_mod;
    function __construct($template_mod = "general") {
        parent::__construct();
        if (Celini::getCurrentController() == 'MedicalMaterial') {
            $menu =& Menu::getInstance();
            $menu->currentSection = 'admin';
        }
        $this->template_mod = $template_mod;

        $this->view->path = 'material';
    }

     /**
     * Get default usage, price coresponding to a given insurance type case of a medicine
     *
     * @param string $medicineName Name of the medicine
     * @param unknown_type $insuranceType Insurance type
     * @return false if no medicine existing
     *  or else, return an array of fields
     */
    public function getMaterialInfo($materialName, $insuranceType='no'){
        $obj = Celini::newORDO('MedicalMaterial');
        $materialInfo = $obj->checkForExistingMaterialName($materialName);
        if ($materialInfo === false) return false;
        if($insuranceType == 'no' || is_null($materialInfo['insurance_support_id'])){
        	$materialInfo['insurance_price']   =   0;
        }
        else {
        	$materialInfo['insurance_support']   =   'yes';
        }

        if (is_null($materialInfo['price'])){
            $materialInfo['unit_price'] =   0;
        }
        else {
            $materialInfo['unit_price'] =   $materialInfo['price'];
        }

        $materialInfo['original_price']     =   $materialInfo['unit_price'];
        $materialInfo['patient_price']      =   $materialInfo['original_price'] - $materialInfo['insurance_price'];
        return json_encode($materialInfo);
    }

    function actionGetAllMaterialNames(){
        $obj = Celini::newORDO('MedicalMaterial');
        $q = strtolower($_GET['q']);
        if (!$q || strlen($q) < 1) return;
        $names = $obj->getMaterialSuggestionNames($q);
        foreach ($names as $value) {
            echo "$value\n";
        }
    }
}
?>